﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Card.Manager.Controls
{
    public partial class DispartPage : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #region 属性
        /// <summary>
        /// 获取或设置记录数
        /// </summary>
        public int RecordCount
        {
            get
            {
                object count = ViewState["RecordCount"];
                if (count == null)
                {
                    return 0;
                }
                else
                {
                    return (int)count;
                }
            }
            set { ViewState["RecordCount"] = value; }
        }

        /// <summary>
        /// 获取或设置每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                object size = ViewState["PageSize"];
                if (size == null)
                {
                    return 10;
                }
                else
                {
                    return (int)size;
                }
            }
            set { ViewState["PageSize"] = value; }
        }

        /// <summary>
        /// 获取或设置页码
        /// </summary>
        /// <remarks>页码从0开始计数</remarks>
        public int PageIndex
        {
            get
            {
                if (PageCount == 0)
                {
                    return -1;
                }
                object index = ViewState["PageIndex"];
                int pageIndex = 0;
                if (index != null)
                {
                    pageIndex = (int)index;
                }
                if (pageIndex > PageCount - 1)
                {
                    ViewState["PageIndex"] = pageIndex = PageCount - 1;
                }
                return pageIndex;
            }
            set
            {
                if (value > PageCount - 1)
                {
                    value = PageCount - 1;
                }
                if (value < 0)
                {
                    value = 0;
                }
                if (PageCount == 0)
                {
                    value = -1;
                }
                ViewState["PageIndex"] = value;
            }
        }

        /// <summary>
        /// 获取页数
        /// </summary>
        public int PageCount
        {
            get { return (((RecordCount + PageSize) - 1) / PageSize); }
        }

        /// <summary>
        /// 获取当前是否位于首页
        /// </summary>
        public bool IsFirstPage
        {
            get { return (PageIndex == 0); }
        }

        /// <summary>
        /// 获取当前是否位于末页
        /// </summary>
        public bool IsLastPage
        {
            get { return (PageIndex == (PageCount == 0 ? 0 : (PageCount - 1))); }
        }

        /// <summary>
        /// 获取当前页中的第一条记录在所有记录中的序号
        /// </summary>
        public int FirstIndexInPage
        {
            get { return (PageIndex * PageSize); }
        }

        /// <summary>
        /// 获取当前页中的最后一条记录在所有记录中的序号

        /// </summary>
        public int LastIndexInPage
        {
            get { return Math.Min(FirstIndexInPage + PageSize, RecordCount); }
        }

        /// <summary>
        /// 获取当前页中实际的记录数
        /// </summary>
        public int Count
        {
            get
            {
                if (IsLastPage)
                {
                    return (RecordCount - FirstIndexInPage);
                }
                return PageSize;
            }
        }

        /// <summary>
        /// 获取图片路径
        /// </summary>
        public string ImagePath
        {
            get
            {
                object path = ViewState["ImagePath"];
                if (path == null)
                {
                    return @"~/Images/";
                }
                else
                {
                    string imagePath = (string)path;
                    if (imagePath.Trim().Length > 0 && !imagePath.Trim().EndsWith("/") &&
                        !imagePath.Trim().EndsWith("\\"))
                    {
                        imagePath += "/";
                    }
                    return imagePath;
                }
            }
            set { ViewState["ImagePath"] = value; }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 当页码发生变化时所触发的事件

        /// </summary>
        public event PageIndexChangeHandler PageIndexChange;


        #endregion

        protected virtual void OnPageIndexChange(PageIndexChangeEventArgs e)
        {
            PageIndex = e.NewPageIndex;
            if (PageIndexChange != null)
            {
                PageIndexChange(this, e);
            }
        }


        #region 重载方法

        protected override void OnPreRender(EventArgs e)
        {
            lblRecordCount.Text = string.Format("共{0}条", RecordCount.ToString());
            lblTotal.Text = string.Format("共{0}页", PageCount.ToString());

            btnFirst.Enabled = !IsFirstPage && PageCount > 0;
            btnPrev.Enabled = !IsFirstPage && PageCount > 0;
            btnNext.Enabled = !IsLastPage && PageCount > 0;
            btnLast.Enabled = !IsLastPage && PageCount > 0;

            btnFirst.Style.Add(HtmlTextWriterStyle.Cursor, btnFirst.Enabled ? "hand" : "default");
            btnPrev.Style.Add(HtmlTextWriterStyle.Cursor, btnPrev.Enabled ? "hand" : "default");
            btnNext.Style.Add(HtmlTextWriterStyle.Cursor, btnNext.Enabled ? "hand" : "default");
            btnLast.Style.Add(HtmlTextWriterStyle.Cursor, btnLast.Enabled ? "hand" : "default");


            btnFirst.ToolTip = btnFirst.Enabled ? "首页" : "";
            btnPrev.ToolTip = btnPrev.Enabled ? "上一页" : "";
            btnNext.ToolTip = btnNext.Enabled ? "下一页" : "";
            btnLast.ToolTip = btnLast.Enabled ? "末页" : "";

            // 设置控件的图像路径



            btnFirst.ImageUrl = ImagePath + (btnFirst.Enabled ? "first.gif" : "disable_first.gif");
            btnPrev.ImageUrl = ImagePath + (btnPrev.Enabled ? "back.gif" : "disable_back.gif");
            btnNext.ImageUrl = ImagePath + (btnNext.Enabled ? "next.gif" : "disable_next.gif");
            btnLast.ImageUrl = ImagePath + (btnLast.Enabled ? "last.gif" : "disable_last.gif");

            // 加载页码跳转的下拉框
            dropPage.Items.Clear();
            for (int i = 0; i < PageCount; i++)
            {
                dropPage.Items.Add(new ListItem((i + 1).ToString()));
            }
            if (PageCount == 0)
            {
                dropPage.Items.Add(new ListItem((1).ToString()));
            }
            dropPage.SelectedIndex = PageIndex;
            dropPage.Enabled = PageCount > 0;

            base.OnPreRender(e);
        }

        #endregion

        #region 内部事件处理

        protected void dropPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnPageIndexChange(new PageIndexChangeEventArgs(dropPage.SelectedIndex,
                                                           PageIndex));
        }

        protected void btnNext_Click(object sender, ImageClickEventArgs e)
        {
            OnPageIndexChange(new PageIndexChangeEventArgs(PageIndex + 1,
                                                           PageIndex));
        }

        protected void btnPrev_Click(object sender, ImageClickEventArgs e)
        {
            OnPageIndexChange(new PageIndexChangeEventArgs(PageIndex - 1,
                                                           PageIndex));
        }

        protected void btnFirst_Click(object sender, ImageClickEventArgs e)
        {
            OnPageIndexChange(new PageIndexChangeEventArgs(0, PageIndex));
        }

        protected void btnLast_Click(object sender, ImageClickEventArgs e)
        {
            OnPageIndexChange(new PageIndexChangeEventArgs(PageCount - 1,
                                                           PageIndex));
        }

        #endregion
    }
}